import { Context } from 'koa'
import { insert, select, success } from '../utils'
import { devGit, testGit, onlineGit } from '../utils/git'

export class commitController {
  // 获取本地提交记录
  static async getCommitLogs(ctx: Context, isRoute: any) {
    const res = await devGit[ctx.request.headers.nickname as any].getLocalCommitLogs()
    if (isRoute === false) return res
    ctx.body = success(ctx, res)
  }
  // 获取远程提交记录
  static async getRemoteCommitLogs(ctx: Context, isRoute: any) {
    const { branchId: id, targetBranch }: { branchId: number; targetBranch: 'temp' | 'master' } = ctx.request
      .body as any
    let res
    let branch
    if (!targetBranch) {
      const [result]: [any, any] = (await select('branches', { id })) as [any, any]
      branch = result[0].branch
      res = await devGit[ctx.request.headers.nickname as any].getRemoteCommitLogs(branch)
    } else {
      branch = targetBranch
      const git = targetBranch === 'temp' ? testGit : onlineGit
      res = await git.getRemoteCommitLogs(branch)
    }
    if (isRoute === false) return res
    ctx.body = success(ctx, res as any)
  }
}
